package daos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import persistence.Message;
import persistence.TypeUtilisateur;
import persistence.Utilisateur;
import tools.DataExchangeManager;
import tools.QueryManager;

public class DaoUtilisateur extends AbstractDao<Utilisateur>{

	public DaoUtilisateur() {
		this.setTableName("utilisateurs");
	}

	public DaoUtilisateur(Connection c) {
		super(c);
		this.setTableName("utilisateurs");
	}

	@Override
	public int create(Utilisateur objet) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Utilisateur read() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Utilisateur update(Utilisateur object) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean delete() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Utilisateur findById(int id) {
		//String query = getRequetes().getProperty("req.user.findById");
		String query = "";
		PreparedStatement ps;
		Utilisateur u = new Utilisateur();
		try {
			ps = this.getConnexion().prepareStatement(query);
			ps.setInt(1,id);
			ResultSet rs = ps.executeQuery();
			System.out.println("Resultats");
			while (rs.next()){
				u.setId(id);
				u.setLogin(rs.getString("login"));
				u.setMdp(rs.getString("mdp"));
				u.setNom(rs.getString("nom"));
				u.setPrenom(rs.getString("prenom"));
				u.setType(TypeUtilisateur.valueOf(rs.getString("type")));
			}
		} catch (SQLException e) {
			logger.error(e);
		}
		return null;
	}

	@Override
	public ArrayList<Utilisateur> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArrayList<Utilisateur> find(Utilisateur objet) {
		//Enlever case
		/*String query = "SELECT * FROM "+this.getTableName()+" " +
		"WHERE nom like ? or prenom like ? " +
		"or login like ? or typeutilisateur = ? ";*/
		String query = QueryManager.getRessource("req.user.find");
		//String query = getRequetes().getProperty("req.user.find");
		PreparedStatement ps;
		try {
			ps = this.getConnexion().prepareStatement(query);
			ps.setString(1,objet.getNom());
			ps.setString(2,objet.getPrenom());
			ps.setString(3,objet.getLogin());
			ps.setString(4,objet.getType().name());
			ResultSet rs = ps.executeQuery();
			System.out.println("Resultats");
			while (rs.next()){
				int id = rs.getInt("id");
				System.out.println("Id :"+id);
			}
		} catch (SQLException e) {
			logger.error(e);
		}
		return null;
	}
	
	public Message checkConnexionInfos(Utilisateur u){
		//String query = "SELECT * FROM "+this.getTableName()+" " +
		//"WHERE login=? and mdp=?";
		String query = QueryManager.getRessource("req.user.checkinfos");
		//String query = getRequetes().getProperty("req.user.checkinfos");
		System.out.println(query);
		PreparedStatement ps;
		try {
			ps = this.getConnexion().prepareStatement(query);
			ps.setString(1,u.getLogin());
			ps.setString(2,u.getMdp());
			ResultSet rs = ps.executeQuery();
			System.out.println("Resultats");
			while (rs.next()){
				u.setId(rs.getInt("id"));
				u.setNom(rs.getString("nom"));
				u.setType(TypeUtilisateur.valueOf(rs.getString("typeutilisateur")));
			}
		} catch (SQLException e) {logger.error(e);}
		System.out.println(u.getType());
		String action = "connexion";
		Message resp = new Message();
		resp.setAction(action);
		resp.setType("reponse");		
		resp.setObjet(u);
		//System.out.println(DataExchangeManager.convertToXMLString(resp));
		return resp;
	}
	
	public String checkConnexionInfos(String user){
		String query = QueryManager.getRessource("req.user.checkinfos");
		//String query = getRequetes().getProperty("req.user.checkinfos");
		System.out.println("User received:"+user);
		Message msgIn = null;
		try {
			msgIn = DataExchangeManager.xmlStringToBean(user);
		} catch (Exception e1) {
			logger.error(e1);
		}
		Utilisateur u = (Utilisateur) msgIn.getObjet();
		System.out.println("User received transformed:"+u.getLogin());
		PreparedStatement ps;
		try {
			ps = this.getConnexion().prepareStatement(query);
			ps.setString(1,u.getLogin());
			ps.setString(2,u.getMdp());
			ResultSet rs = ps.executeQuery();
			System.out.println("Resultats");
			while (rs.next()){
				u.setId(rs.getInt("id"));
				u.setNom(rs.getString("nom"));
				u.setType(TypeUtilisateur.valueOf(rs.getString("typeutilisateur")));
			}
		} catch (SQLException e) {logger.error(e);}
		System.out.println(u.getType());
		String action = "connexion";
		Message resp = new Message();
		resp.setAction(action);
		resp.setType("reponse");		
		resp.setObjet(u);
		String strOut = null;
		try {
			strOut = DataExchangeManager.beanToXMLString(resp);
		} catch (Exception e) {
			logger.error(e);
		}
		return strOut;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}
/*
	public String checkConnexionInfosString(String u) {
		// TODO Auto-generated method stub
		String query = QueryManager.getRessource("req.user.checkinfos");
		//String query = getRequetes().getProperty("req.user.checkinfos");
		System.out.println(query);
		PreparedStatement ps;
		try {
			ps = this.getConnexion().prepareStatement(query);
			ps.setString(1,u.getLogin());
			ps.setString(2,u.getMdp());
			ResultSet rs = ps.executeQuery();
			System.out.println("Resultats");
			while (rs.next()){
				u.setId(rs.getInt("id"));
				u.setNom(rs.getString("nom"));
				u.setType(TypeUtilisateur.valueOf(rs.getString("typeutilisateur")));
			}
		} catch (SQLException e) {logger.error(e);}
		System.out.println(userParam.getType());
		String action = "connexion";
		Message resp = new Message();
		resp.setAction(action);
		resp.setType("reponse");		
		resp.setObjet(u);
		return null;
	}
	*/
}
